package com.fivebrms.hibernate.entity.business;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.GregorianCalendar;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.fivebrms.forms.w9.forms.RemoteW9Form;
import com.fivebrms.hibernate.entity.dao.W9FormDaoHibernate;
import com.fivebrms.hibernate.entity.model.W9Form;

public class W9FormService
{
	final Logger	log	= LoggerFactory.getLogger(W9FormService.class);

	public RemoteW9Form sendW9(RemoteW9Form w9Form)
	{
		log.info("sendW9..ok successfully called the logger.. " + w9Form);

		ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext
		        .getServletConfig().getServletContext());

		log.info("form created..2..");

		String productDAOName = "productDAO";
		String w9FormDAOName = "w9FormDAO";

		log.info("form created..3..");

		log.info("hW9FormDao created..1..");

		W9FormDaoHibernate hW9FormDao = (W9FormDaoHibernate) appContext.getBean(w9FormDAOName);
		log.info("hW9FormDao created..2..");

		W9Form w9FormM = new W9Form();
		try
		{
			// BeanUtils.setProperty(w9FormM, "contNum", w9Form.isBusiness());
			BeanUtils.setProperty(w9FormM, "pin", "");
			BeanUtils.setProperty(w9FormM, "dateCreated", new Timestamp(GregorianCalendar.getInstance().getTimeInMillis()));
			BeanUtils.setProperty(w9FormM, "dateModifiedLast", new Timestamp(GregorianCalendar.getInstance().getTimeInMillis()));
			BeanUtils.setProperty(w9FormM, "status", "E");

			BeanUtils.setProperty(w9FormM, "individual", w9Form.isIndividual());
			BeanUtils.setProperty(w9FormM, "partnership", w9Form.isPartnership());
			BeanUtils.setProperty(w9FormM, "indivPartName", w9Form.getIndivPartName());
			BeanUtils.setProperty(w9FormM, "indivPartSsNum", w9Form.getIndivPartSsNum());

			BeanUtils.setProperty(w9FormM, "corporation", w9Form.isCorporation());
			BeanUtils.setProperty(w9FormM, "business", w9Form.isBusiness());
			BeanUtils.setProperty(w9FormM, "corpBusName", w9Form.getCorpBusName());
			BeanUtils.setProperty(w9FormM, "corpBusIdentification", w9Form.getCorpBusIdentification());
			BeanUtils.setProperty(w9FormM, "corpBusAddress", w9Form.getCorpBusAddress());

			BeanUtils.setProperty(w9FormM, "signature", w9Form.getSignature());
		}
		catch (IllegalAccessException iae)
		{
			log.error("an error IllegalAccessException...", iae);
		}
		catch (InvocationTargetException ite)
		{
			log.error("an error occurred...", ite);
		}
		
		log.info("hW9FormDao created..3..");

		hW9FormDao.createW9Form(w9FormM);
		log.info("hW9FormDao created..4..");

		Collection<W9Form> coll = hW9FormDao.findAll();
		log.info("coll size coll size coll size.." + coll.size());

		log.info("sendW9..finished successfully.. ");
		log.info("form created..4..");
		log.info("sendW9..finished successfully.. ");

		return w9Form;
	}

	public boolean update(RemoteW9Form w9Form)
	{

		System.out.println("form updated..");

		return true;
	}

}
